package com.traveling.hztraveling.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.traveling.hztraveling.context.BaseContext;
import com.traveling.hztraveling.entity.po.HzUser;
import com.traveling.hztraveling.entity.vo.Result;
import com.traveling.hztraveling.mapper.HzUserMapper;
import com.traveling.hztraveling.service.IHzUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LHD
 * @since 2025-05-29
 */
@Service
@RequiredArgsConstructor
public class HzUserServiceImpl extends ServiceImpl<HzUserMapper, HzUser> implements IHzUserService {

    private final HzUserMapper hzUserMapper;


    @Override
    @Transactional
    public Integer login(HzUser hzUser){

        Wrapper<HzUser> queryWrapper = new QueryWrapper<HzUser>()
                .eq("username",hzUser.getUsername())
                .eq("password",hzUser.getPassword());
        HzUser hzUser1 = hzUserMapper.selectOne(queryWrapper);
        if (hzUser1 == null){
            return 0;
        }
        else {
            if (hzUser1.getRole() == 100){
                return 100;
            }
            return hzUser1.getId();
        }
    }

    @Override
    @Transactional
    public Result<HzUser> register(HzUser hzUser){
        List<HzUser> list = this.lambdaQuery()
                .eq(HzUser::getUsername, hzUser.getUsername())
                .list();
        if (!list.isEmpty()){
            return Result.fail("用户名已存在");
        }

        boolean save = this.save(hzUser);
        if (save){
            return Result.success(hzUser);
        }
        else {
            return Result.fail("注册失败");
        }
    }
}
